package com.ebo.kline.util.index;

import com.ebo.kline.model.IKData;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by admin on 2018/3/12.
 */

public class SMA {

    public static List<? extends IKData> caculate(List<? extends IKData> list) {

        float amountVol = 0;

        List<List<Double>> AllMaList = new ArrayList<>();

        if(list.size()<1) return list;

        for (int k = 0; k < list.get(0).getMACycle().length; k++) {
            AllMaList.add(calculateMA(list.get(0).getMACycle()[k], list));
        }

        List<Double> ma20List = calculateMA(20, list);

        for (int i = 0; i < list.size(); i++) {
            IKData hisData = list.get(i);

            for (int k = 0; k < list.get(0).getMACycle().length; k++) {
                List<Double> maList = AllMaList.get(k);
                hisData.getSma().getSma().put(list.get(0).getMACycle()[k],maList.get(i).doubleValue());
            }

            hisData.getSma().setSma20(ma20List.get(i));
            amountVol += hisData.getVolume();
            hisData.setAmountVol(amountVol);
            if (i > 0) {
                double total = hisData.getVolume() * hisData.getClose_price() + list.get(i - 1).getTotal();
                hisData.setTotal(total);
                double avePrice = total / amountVol;
                hisData.setAvePrice(avePrice);
            } else {
                hisData.setAmountVol((float) hisData.getVolume());
                hisData.setAvePrice(hisData.getClose_price());
                hisData.setTotal(hisData.getAmountVol() * hisData.getAvePrice());
            }

        }
        return list;
    }






    /**
     * calculate MA value, return a double list
     *
     * @param dayCount for example: 5, 10, 20, 30
     */
    public static List<Double> calculateMA(int dayCount, List<? extends IKData> data) {
        List<Double> result = new ArrayList<>(data.size());
        for (int i = 0, len = data.size(); i < len; i++) {
            if (i < dayCount) {
                result.add(Double.NaN);
                continue;
            }
            double sum = 0;
            for (int j = 0; j < dayCount; j++) {
                sum += data.get(i - j).getOpen_price();
            }
            result.add(+(sum / dayCount));
        }
        return result;
    }

    /**
     * calculate last MA value, return a double value
     */
    public static double calculateLastMA(int dayCount, List<? extends IKData> data) {
        double result = Double.NaN;
        for (int i = 0, len = data.size(); i < len; i++) {
            if (i < dayCount) {
                result = Double.NaN;
                continue;
            }
            double sum = 0;
            for (int j = 0; j < dayCount; j++) {
                sum += data.get(i - j).getOpen_price();
            }
            result = (+(sum / dayCount));
        }
        return result;
    }


    /**
     * according to the history data list, calculate a new data
     */
//    public IKData calculateHisData(IKData newData, List<IKData> hisDatas,KConfig config) {
//
//        List<List<Double>> AllMaList = new ArrayList<>();
//
//        IKData lastData = hisDatas.get(hisDatas.size() - 1);
//        float amountVol = lastData.getAmountVol();
//
//        for (int k = 0; k < config.SMA.getMACount(); k++) {
//            newData.getSma().getSma().add(calculateLastMA(config.SMA.getMA(k).getIndex(), hisDatas));
//        }
//
//        amountVol += newData.getVolume();
//        newData.setAmountVol(amountVol);
//
//        double total = newData.getVolume() * newData.getClose_price() + lastData.getTotal();
//        newData.setTotal(total);
//        double avePrice = total / amountVol;
//        newData.setAvePrice(avePrice);
//
//        return newData;
//    }

}
